package GUI;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTable;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTextField;

import simulation.ROB;
import simulation.ReservationStation;

import java.awt.Color;


public class HardwareOrganization extends JFrame implements ActionListener{

	private JPanel contentPane;
	private JTextField textFieldAddCycles;
	private JTextField textFieldMultiplyCycles;
	private JTextField textFieldDivideCycles;
	private JTextField textFieldPipelineWidth;
	private JTextField textFieldQueueSize;
	private JTextField textFieldReservationStations;
	private JTextField textFieldROBEntries;
	JButton btnNext;
	JLabel lblError;
	String memoryCapacity;
	JComboBox comboBoxAdd ;
	JComboBox comboBoxMultiply;
	JComboBox comboBoxDivide;
	MemoryHierarchy mh;

	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					HardwareOrganization frame = new HardwareOrganization("512",null);
//					frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the frame.
	 */
	public HardwareOrganization(String m,MemoryHierarchy mh) {
		this.mh=mh;
		memoryCapacity = m;
		setResizable(false);
		setTitle("Hardware Organization");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 340);
		contentPane = new JPanel();
		contentPane.setToolTipText("");
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		btnNext = new JButton("Next");
		btnNext.setBounds(323, 268, 89, 23);
		btnNext.addActionListener(this);
		contentPane.add(btnNext);
		
		JLabel PipelineWidthLabel = new JLabel("Pipeline Width ");
		PipelineWidthLabel.setToolTipText("the number of instructions that can be dispatched to the reservation \r\nstations simultaneously under ideal circumstances");
		PipelineWidthLabel.setBounds(23, 51, 89, 23);
		contentPane.add(PipelineWidthLabel);
		
		JLabel QueueSizeLabel = new JLabel("Queue Size");
		QueueSizeLabel.setToolTipText("size \r\nof \r\nthe \r\ninstruction \r\nbuffer");
		QueueSizeLabel.setHorizontalAlignment(SwingConstants.CENTER);
		QueueSizeLabel.setBounds(103, 51, 73, 23);
		contentPane.add(QueueSizeLabel);
		
		JLabel ReservationStationsLabel = new JLabel("Reservation\r\n Stations");
		ReservationStationsLabel.setBounds(191, 51, 125, 23);
		contentPane.add(ReservationStationsLabel);
		
		JLabel RobEntriesLabel = new JLabel("ROB entries");
		RobEntriesLabel.setBounds(326, 51, 73, 23);
		contentPane.add(RobEntriesLabel);
		
		JLabel lblUnits = new JLabel("Units");
		lblUnits.setHorizontalAlignment(SwingConstants.CENTER);
		lblUnits.setBounds(20, 172, 43, 14);
		contentPane.add(lblUnits);
		
		JLabel lblCycles = new JLabel("Cycles");
		lblCycles.setToolTipText("the \r\nnumber \r\nof \r\ncycles \r\nneeded \r\nby \r\neach \r\nunit");
		lblCycles.setHorizontalAlignment(SwingConstants.CENTER);
		lblCycles.setBounds(20, 213, 43, 14);
		contentPane.add(lblCycles);
		
		JLabel lblAdd = new JLabel("Add / Sub");
		lblAdd.setHorizontalAlignment(SwingConstants.CENTER);
		lblAdd.setBounds(58, 147, 89, 14);
		contentPane.add(lblAdd);
		
		JLabel lblMult = new JLabel("Mult");
		lblMult.setHorizontalAlignment(SwingConstants.CENTER);
		lblMult.setBounds(153, 147, 47, 14);
		contentPane.add(lblMult);
		
		JLabel lblDiv = new JLabel("Div");
		lblDiv.setHorizontalAlignment(SwingConstants.CENTER);
		lblDiv.setBounds(232, 147, 44, 14);
		contentPane.add(lblDiv);
		
		comboBoxAdd = new JComboBox();
		comboBoxAdd.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17"}));
		comboBoxAdd.setBounds(83, 169, 43, 20);
		contentPane.add(comboBoxAdd);
		
		comboBoxMultiply = new JComboBox();
		comboBoxMultiply.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17"}));
		comboBoxMultiply.setBounds(157, 169, 43, 20);
		contentPane.add(comboBoxMultiply);
		
		comboBoxDivide = new JComboBox();
		comboBoxDivide.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17"}));
		comboBoxDivide.setBounds(232, 169, 43, 20);
		contentPane.add(comboBoxDivide);
		
		textFieldAddCycles = new JTextField();
		textFieldAddCycles.setBounds(83, 210, 43, 20);
		contentPane.add(textFieldAddCycles);
		textFieldAddCycles.setColumns(3);
		
		textFieldMultiplyCycles = new JTextField();
		textFieldMultiplyCycles.setColumns(3);
		textFieldMultiplyCycles.setBounds(157, 210, 43, 20);
		contentPane.add(textFieldMultiplyCycles);
		
		textFieldDivideCycles = new JTextField();
		textFieldDivideCycles.setColumns(3);
		textFieldDivideCycles.setBounds(232, 210, 43, 20);
		contentPane.add(textFieldDivideCycles);
		
		textFieldPipelineWidth = new JTextField();
		textFieldPipelineWidth.setBounds(20, 85, 73, 20);
		contentPane.add(textFieldPipelineWidth);
		textFieldPipelineWidth.setColumns(10);
		
		textFieldQueueSize = new JTextField();
		textFieldQueueSize.setColumns(10);
		textFieldQueueSize.setBounds(113, 85, 63, 20);
		contentPane.add(textFieldQueueSize);
		
		textFieldReservationStations = new JTextField();
		textFieldReservationStations.setColumns(10);
		textFieldReservationStations.setBounds(206, 85, 80, 20);
		contentPane.add(textFieldReservationStations);
		
		textFieldROBEntries = new JTextField();
		textFieldROBEntries.setColumns(10);
		textFieldROBEntries.setBounds(319, 85, 80, 20);
		contentPane.add(textFieldROBEntries);
		
		lblError = new JLabel("Please choose applicable values");
		lblError.setVisible(false);
		lblError.setForeground(Color.RED);
		lblError.setBounds(20, 268, 259, 23);
		contentPane.add(lblError);
		
		JLabel lblRequired = new JLabel("* All fields are required ");
		lblRequired.setForeground(Color.RED);
		lblRequired.setBounds(208, 11, 226, 23);
		contentPane.add(lblRequired);
		
		this.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==btnNext)
		{
			if(checkFields())
			{
				//assigning the values
				int addUnits = Integer.parseInt((String) comboBoxAdd.getSelectedItem());
				int mulUnits = Integer.parseInt((String) comboBoxMultiply.getSelectedItem());
				int divUnits = Integer.parseInt((String) comboBoxDivide.getSelectedItem());
				
				
				this.dispose();
				new ProgramAndData(memoryCapacity,this);
			}
			else
			{
				lblError.setVisible(true);
			}
		}
	}

	private boolean checkFields() {
		try
		{
			int a = Integer.parseInt(textFieldAddCycles.getText());
			int d = Integer.parseInt(textFieldDivideCycles.getText());
			int m = Integer.parseInt(textFieldMultiplyCycles.getText());
			int w = Integer.parseInt(textFieldPipelineWidth.getText());
			int q  = Integer.parseInt(textFieldQueueSize.getText());
			int ro = Integer.parseInt(textFieldROBEntries.getText());
			int re = Integer.parseInt(textFieldReservationStations.getText());
			mh.simulator.reservationStations = new ReservationStation(re, a, m, d);
			mh.simulator.rob = new ROB(ro);
			mh.simulator.ways = w ;
		}
		catch (NumberFormatException e) {
			return false;
		}
		return true;
	}
}
